{extend name="layout" /}
{block name="head"}
<style type="text/css">
    .post_upload{
        position: absolute;
        top: 0px;
        left: 0px;
        opacity: 0;
        cursor: pointer;
        outline: none;
        z-index: 10;
    }
    .upload_img{
        width: 180px;
        height: 140px;
    }
</style>
<!-- include summernote css/js-->
<link type="text/css" href="__STATIC__/summernote/summernote.css" rel="stylesheet">
<script type="text/javascript" src="__STATIC__/summernote/summernote.min.js"></script>
<!-- include summernote-zh-CN -->
<script src="__STATIC__/summernote/lang/summernote-zh-CN.js"></script>
<script type="text/javascript">
    var summer;
    $(document).ready(function() {
        summer = $('#summernote');
        summer.summernote({
            height: 300, // set editor height
            minHeight: 300, // set minimum height of editor
            lang: 'zh-CN', // default: 'en-US'
            callbacks: {
                onImageUpload: function(files) {
                    sendFile(files);
                }
            }
        });
    });
    /** * 发送图片文件给服务器端 */
    function sendFile(files) {
        let imageData = new FormData();
        for (var i=0;i<files.length;i++){
            imageData.append("img[]", files[i]);
        }
        $.ajax({
            url: "{:url('notice/updateFile')}", // 图片上传url
            type: 'POST',
            data: imageData,
            cache: false,
            contentType: false,
            processData: false,
            dataType:'json',     // 以json的形式接收返回的数据
            success: function ($result) {
                for (var num = 0; num < $result.data.length; num++) {
                    var imgNode = document.createElement("img");
                    imgNode.src = '__SUMMERNOTE__'+$result.data[num];
                    summer.summernote('insertNode', imgNode);
                }
            },
            error: function () {
                console.log('图片上传失败');
            }
        });
    }
</script>
{/block}
{block name="content"}
<div class="table-margin">
    <form action="{:url('product/edit')}" class="form-horizontal" method="post">
        <input type="hidden" name="id" value="{$product.id?:''}">
        <table class="table table-bordered contact-template-form">
            <tbody>
            <tr>
                <td width="20%" align="right"><font color="red">*</font>商品分类：</td>
                <td>
                    <select name="cat_id" class="form-control">
                        {foreach name="role" item="r"}
                        <option value="{$r.id}">{$r.role_name}</option>
                        {/foreach}
                    </select>
                </td>
            </tr>
            <tr>
                <td width="20%" align="right"><font color="red">*</font>名称：</td>
                <td>
                    <input class="form-control" type="text" name="title" required value="{$product.name?:''}">
                </td>
            </tr>
            <tr>
                <td width="20%" align="right"><font color="red">*</font>销售价：</td>
                <td>
                    <input class="form-control decimal" type="text" name="price" required value="{$product.price?:''}" oninput="value=value.replace(/[^0-9.]/g,'');">
                </td>
            </tr>
            <tr>
                <td width="20%" align="right"><font color="red">*</font>成本价：</td>
                <td>
                    <input class="form-control decimal" type="text" name="cost_price" required value="{$product.cost_price?:''}" oninput="value=value.replace(/[^0-9.]/g,'');">
                </td>
            </tr>
            <tr>
                <td width="20%" align="right"><font color="red">*</font>原价：</td>
                <td>
                    <input class="form-control decimal" type="text" name="original_price" required value="{$product.original_price?:''}" oninput="value=value.replace(/[^0-9.]/g,'');">
                </td>
            </tr>
            <tr>
                <td width="20%" align="right"><font color="red">*</font>单位：</td>
                <td>
                    <input class="form-control" type="text" name="unit" required value="{$product.unit?:''}">
                </td>
            </tr>
            <tr>
                <td width="20%" align="right"><font color="red">*</font>重量：</td>
                <td>
                    <input class="form-control decimal" type="text" name="weight" required value="{$product.weight?:''}" oninput="value=value.replace(/[^0-9.]/g,'');">
                </td>
            </tr>
            <tr>
                <td width="20%" align="right"><font color="red">*</font>缩略图：</td>
                <td>
                    <div style="position: relative;">
                        <img class="upload_img" src="{$product.img?'__UPLOAD__'.$product.img:'__IMG__/game.png'}" alt="" />
                        <input type="file" name="logo" value="" class="post_upload upload_img" onchange="loadImg1(this)" />
                    </div>
                </td>
            </tr>
            <tr>
                <td width="20%" align="right"><font color="red">*</font>内容：</td>
                <td>
                    <textarea id="summernote" name="content">{$product.content?:''}</textarea>
                </td>
            </tr>
            <tr>
                <td width="20%" align="right"><font color="red">*</font>库存：</td>
                <td>
                    <input class="form-control" type="text" name="stock" required value="{$product.stock?:''}" oninput="value=value.replace(/[^0-9]/g,'');">
                </td>
            </tr>
            <tr>
                <td width="20%" align="right"><font color="red">*</font>虚拟销量：</td>
                <td>
                    <input class="form-control" type="text" name="virtual_num" required value="{$product.virtual_num?:''}" oninput="value=value.replace(/[^0-9]/g,'');">
                </td>
            </tr>
            <tr>
                <td width="20%" align="right"><font color="red">*</font>限购：</td>
                <td>
                    <input class="form-control" type="text" name="limit" required value="{$product.limit?:''}" oninput="value=value.replace(/[^0-9]/g,'');">
                </td>
            </tr>
            <tr>
                <td width="20%" align="right">团长抽成设置：</td>
                <td>
                    {foreach name="Think.config.leader" item="s"}
                    <label>
                        <input class="xb-icheck" type="radio" name="leader_draw_type" {$key==1?='checked'} value="{$key}">
                        <span class="pull-right">&nbsp;{$s}&nbsp;&nbsp;&nbsp;&nbsp;</span>
                    </label>
                    {/foreach}
                </td>
            </tr>
            <tr>
                <td width="20%" align="right"><font color="red">*</font>团长抽成比率：</td>
                <td>
                    <input class="form-control decimal" type="text" name="leader_draw_rate" required value="{$product.leader_draw_rate?:''}" oninput="value=value.replace(/[^0-9.]/g,'');">
                </td>
            </tr>
            <tr>
                <td width="20%" align="right"><font color="red">*</font>团长抽成金额：</td>
                <td>
                    <input class="form-control decimal" type="text" name="leader_draw_money" required value="{$product.leader_draw_money?:''}" oninput="value=value.replace(/[^0-9.]/g,'');">
                </td>
            </tr>
            <tr>
                <tD></td>
                <td>
                    <input class="btn btn-primary" type="submit" value="提交">
                </td>
            </tr>
            </tbody>
        </table>
    </form>
</div>
<script type="text/javascript">
    function loadImg1(obj){
        var file1 = obj.files[0];
        var reader = new FileReader();
        var imgFile;
        reader.onload=function(e){
            imgFile = e.target.result;
            // var divs = $('.post_upload').children('div');
            $(obj).prev('img').attr('src', imgFile);
            // divs.eq(0).next('div').show();
        };
        reader.readAsDataURL(file1);
    }
    $(function () {
        $('form').submit(function(){
            $(this).ajaxSubmit({
                success:function(res) {
                    if(1 == res.code){
                        layer.alert(res.msg, {
                            title: '成功',
                            skin: 'layui-layer-rim',
                            closeBtn: 0,
                            anim: 4
                        }, function(index){
                            layer.close(index);
                            if(res.data > 0){
                                history.go(-1);
                            }else{
                                location.reload(true);
                            }
                        });
                    }else{
                        layer.alert(res.msg, {icon:2});
                    }
                }
            });
            return false;
        });
        $('.decimal').on('input','input',function (){
            if($(this).val()%1 === 0 && $(this).val().indexOf('.') == -1){
                $(this).val(parseInt($(this).val()==''?0:$(this).val()));
            }else if(!/^[0-9]+.?[0-9]?[0-9]?$/.test($(this).val())){
                var dhq = $(this).val().match(/^[0-9]+.[0-9]?[0-9]?/);
                $(this).val($(this).val()=='.'?0:dhq[0]);
            }
        });
    });
</script>
{/block}